package com.hbuzzs.shiro.config.shiro;

import com.hbuzzs.shiro.entity.MyPermission;
import com.hbuzzs.shiro.entity.UserInfo;
import com.hbuzzs.shiro.service.IRolePermissionService;
import com.hbuzzs.shiro.service.IUserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description shiro配置类
 * @Author zhangzhishun
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CustomRealm extends AuthorizingRealm {

    private final IUserInfoService userInfoService;
    private final IRolePermissionService rolePermissionService;

    /**
     * @param principalCollection 认证主要凭证
     * @return AuthorizationInfo
     * @Description 权限配置类
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String userId = (String) ((Map<String, Object>) principalCollection.getPrimaryPrincipal()).get("userId");
        String roleName = (String) ((Map<String, Object>) principalCollection.getPrimaryPrincipal()).get("roleName");
        List<MyPermission> userInfo = rolePermissionService.getPermissionByUserIdRoleName(userId, roleName);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //添加角色
        simpleAuthorizationInfo.addRole(roleName);
        //添加权限
        userInfo.forEach(e -> simpleAuthorizationInfo.addStringPermission(e.getName()));
        return simpleAuthorizationInfo;
    }

    /**
     * @param authenticationToken [authenticationToken]
     * @return AuthenticationInfo
     * @Description 认证配置类
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (StringUtils.isEmpty(authenticationToken.getPrincipal())) {
            return null;
        }
        //获取用户信息
        String userId = authenticationToken.getPrincipal().toString().split("&&")[0];
        String roleName = authenticationToken.getPrincipal().toString().split("&&")[1];
        // 组装获取用户信息的sql条件
        Map<String, Object> principal = new HashMap<>(16);
        principal.put("userId", userId);
        principal.put("roleName", roleName);
        UserInfo user = userInfoService.getUserInfoByIdRole(principal);
        if (user == null) {
            //这里返回后会报出对应异常
            return null;
        } else {
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            return new SimpleAuthenticationInfo(principal, user.getUserPasswd(), getName());
        }
    }

}
